angular.module('starter.filter',[])
    .filter('abbrMoney', function() {
        return function(value) {

            var temp;

            if (value < 10000) {
                temp = value + '元';
            } else if (value >= 10000 && value < 100000000) {
                temp = value / 10000 + '万';
            } 
            // else if (value >= 1000000 && value < 10000000) {
            //     temp = value / 1000000 + '百万';
            // } else if (value >= 10000000 && value < 100000000) {
            //     temp = value / 10000000 + '千万';
            // } 
            else if (value >= 100000000 && value < 100000000000) {
                temp = value / 100000000 + '亿';
            } 
            // else if (value >= 10000000000) {
            //     temp = value / 10000000000 + '百亿';
            // } else if (value >= 100000000000){
            //     temp = value / 100000000000 + '千亿';
            // }

            return temp;
        }
    })
    .filter('abbrMoney2', function() {
        return function(value) {

            var temp;
            if(value>=0){
                temp = value / 10000 + '万';
            }
            //if (value < 10000) {
            //    temp = value + '元';
            //} else
            // if (value >= 10000 && value < 100000000) {
            //    temp = value / 10000 + '万';
            //}
            // else if (value >= 1000000 && value < 10000000) {
            //     temp = value / 1000000 + '百万';
            // } else if (value >= 10000000 && value < 100000000) {
            //     temp = value / 10000000 + '千万';
            // }
            //else if (value >= 100000000 && value < 100000000000) {
            //    temp = value / 100000000 + '亿';
            //}
            // else if (value >= 10000000000) {
            //     temp = value / 10000000000 + '百亿';
            // } else if (value >= 100000000000){
            //     temp = value / 100000000000 + '千亿';
            // }

            return temp;
        }
    })
    .filter('pSizeRatioType', function() {
        return function(value) {

            var temp;

            if (value === '-') {
                temp = value;
            } else if (value === '01') {
                temp = '小额畅打';
            } else if (value === '02') {
                temp = '已配出小额';
            } else if (value === '03') {
                temp = '严格配比';
            } else if (value === '04') {
                temp = '全大额';
            }

            return temp;
        }
    })
    .filter('cRiskScore', function() {
        return function(value) {
            var temp;
            if (value === -1) {
                temp = '未做评估';
            } else if (value > 9 && value < 14) {
                temp = '保守型';
            } else if (value > 13 && value < 28) {
                temp = '稳健型';
            } else if (value > 27 && value < 41) {
                temp = '积极型';
            }
            return temp;
        }
    })
    .filter('cRiskScoreDesc', function() {
        return function(value) {
            var temp;
            if (value === -1) {
                temp = '未做评估';
            } else if (value > 9 && value < 14) {
                temp = '您的投资目标是寻求资本的保值，其次为资本的缓和升值，可承受的风险较低。';
            } else if (value > 13 && value < 28) {
                temp = '您的投资目标是寻求资本的缓和升值，其次为资本保值，可承担中等风险。';
            } else if (value > 27 && value < 41) {
                temp = '您的投资目标是财富快速增长，您可承受较高风险，了解高收益与高风险相伴随。';
            }
            return temp;
        }
    })
    .filter('faxingdiqu', function() {
        return function(value) {

            var temp;

             if (value === '00') {
                temp = '全国';
            } else if (value === '01') {
                temp = '江浙沪';
            } else if (value === '02') {
                temp = '京津冀';
            } else if (value === '03') {
                temp = '珠三角';
            } else if (value === '04'){
                temp = '其他地区';
            }

            return temp;
        }
    })
    .filter('zuzhixingshi', function() {
        return function(value) {

            var temp;

             if (value === '01') {
                temp = '信托';
            } else if (value === '02') {
                temp = '券商资管';
            } else if (value === '03') {
                temp = '有限合伙';
            } else if (value === '04') {
                temp = '契约型基金';
            } else if (value === '05'){
                temp = '基金子公司';
            } else if (value === '06'){
                temp = '其他';
            }else{
                temp = value;
            }

            return temp;
        }
    })
    .filter('to_trusted', function($sce) {
        return function(text) {
          return $sce.trustAsHtml(text);
        };
    })
    .filter('getAdUrl', function() {
        return function(url) {
            if(url){
                var _url = url.indexOf('#');
                return url.substr(_url-1);
            }

        }
    })
    .filter('getStatus',function(){
        return function(s){
            var status;
            if(s == '01'){
                status = '未开始';
            }else if(s == '02'){
                status = '预热中';
            }else if(s == '03'){
                status = '募集中';
            }else if(s == '04'){
                status = '已募满';
            }else if(s == '05'){
                status = '募集结束';
            }
            return status;
        }
    })
    .filter('getTypeName',function(){
        return function(s){
            var status;
            if(s == '01'){
                status = '阳光私募';
            }else if(s == '02'){
                status = '集合信托';
            }else if(s == '03'){
                status = '集合资管';
            }else if(s == '07'){
                status = '债权基金';
            }else if(s == '06'){
                status = '股权基金';
            }else {
                status = s;
            }
            return status;
        }
    })
    .filter('getInterestType',function(){
        return function(s){
            var status;
            if(s == '02'){
                status = '按季付息';
            }else if(s == '03'){
                status = '按半年付息';
            }else if(s == '04'){
                status = '按年付息';
            }else if(s == '05'){
                status = '到期还本付息';
            }else{
                status = s;
            }
            return status;
        }
    })
    .filter('getProType',function(){
        return function(s){
            var type;
            if(s == '02' || s == '03'){
                type = 'xtzgdetail';
            }else if(s == '06'){
                type = 'pevc';
            }
            return type;
        }
    })
    .filter('profitRenderStringToInt', function() {
      return function(rate, flag) {
        rate = rate || 0;
        rate = rate.replace('%','');
        if (flag && rate <= 0) return "-";
        if (rate > 100) rate = 100;
        if (/^[\d.]+$/.test(rate)) {
          return (rate || 0);
        } else {
          return rate;
        }
      }
    })
    .filter('profitRender', function() {
      return function(rate, flag) {
        rate = rate || 0;
        if (flag && rate <= 0) return "-";
        if (rate > 100) rate = 100;
        if (/^[\d.]+$/.test(rate)) {
          return (rate || 0) + "%";
        } else {
          return rate;
        }
      }
    })
    .filter('profitRenderReturnInt', function() {
      return function(rate, flag) {
        rate = rate || 0;
        if (flag && rate <= 0) return 0;
        if (rate > 100) rate = 100;
        if (/^[\d.]+$/.test(rate)) {
          return (rate || 0);
        } else {
          return rate;
        }
      }
    })
    .filter('investTypeRender', function($faMAP) {
      return function(v) {
        var l = $faMAP.investType;
        if(l && v){
            return l[v] || '未知';
        }
        return "";
      }
    })
    .filter('getProductType', function() {
      return function(v) {
        var t = '';
        switch (v) {
          case '02':
            t = '集合信托';
            break;
          case '03':
            t = '集合资管';
            break;
        }
        return t;
      }
    })
    .filter('formatDate', function($filter) {
      return function(v) {
        var t = '';
        var fm = 'yyyy年MM月dd日HH时';
        var d;

        if(v){
          v = v.replace(new RegExp(/(-)/g), '/');

          if(typeof v != 'object'){
            d = new Date(v);
          }else{
            d = v;
          }
          t = $filter('date')(d, fm);
        }
        return t;
      }
    })
    .filter('biddingProductStatusRender', function($faMAP) {
      return function(v) {
        var l = $faMAP.biddingProductStatus;
        if(l && v){
            return l[v] || v;
        }
        return ".";
      }
    })
    .filter('orderPayStatusRender', function($faMAP) {
      return function(v) {
        var l = $faMAP.payStatus;
        if(l && v){
            return l[v] || v;
        }
        return ".";
      }
    })
    .filter('updataTypeViewRender', function() {  
        return function(v) {
            var text = "";
            if (v == "01" || v == "1"){
                text = "身份证正面";
            }else if (v == "02" || v == "2"){
                text = "身份证反面";
            }else if (v == "03" || v == "3"){
                return "银行卡";
            }else if (v == "04" || v == "4"){
                text = "打款凭条";
            }else if (v == "05" || v == "5"){
                return "签字页";
            }else if (v == "06" || v == "6"){
                text = "合同封面";
            }else if (v == "07" || v == "7"){
                return "手持签字页与合同封面照";
            }else if (v == "08" || v == "8"){
                text = "其它";
            }
            return text;
        }
    })
    .filter('updataTypeStatusRender', function() {  
        return function(v) {
            var text = "";
            if (v == "01" || v == "1"){
                text = "待上传";
            }else if (v == "02" || v == "2"){
                text = "审核中";
            }else if (v == "03" || v == "3"){
                return "审核通过";
            }else if (v == "04" || v == "4"){
                text = "审核未通过";
            }else if (v == "00" || v == "0"){
                text = "上传成功";
            }else {
                text = "待上传";
            }
            return text;
        }
    })

    .filter('payTypeRender', function(){
        return function(v){
            var text = v;

            if(v == 'paybzj'){
                text = '支付保证金';
            }else if(v == 'payyj'){
                text = '支付佣金';
            }else if(v == 'txbzj'){
                text = '提现保证金';
            }else if(v == 'txyj'){
                text = '提现佣金';
            }

            return text;
        }
    })
    .filter('withdrawAmountType', function(){
        return function(v){
            var text = v;

            if(v == '01'){
                text = '保证金';
            }else if(v == '02'){
                text = '佣金';
            }else if(v == '03'){
                text = '佣金';
            }else if(v == '04'){
                text = '佣金';
            }

            return text;
        }
    })
    .filter('touzhihangye', function($faMAP) {
        return function(v) {
            var l = $faMAP.investType;
            return l[v];
        }
    })
    .filter('payStatusRender', function(){
        return function(v){
            var text = v;

            if(v == '0'){
                text = '提现中';
            }else if(v == '2'){
                text = '提现成功';
            }else if(v == '3'){
                text = '提现失败';
            }else{
                text = v;
            }

            return text;
        }
    })
    .filter('walletStatusRender', function(){
        return function(v){
            var text = v;

            if(v == '0'){
                text = '中';
            }else if(v == '2'){
                text = '成功';
            }else if(v == '3'){
                text = '失败';
            }else{
                text = v;
            }

            return text;
        }
    })

    .filter('depositStatusRender', function(){
        return function(v){
            var text = v;

            if(v == '01'){
                text = '冻结';
            }else if(v == '02'){
                text = '可提现';
            }else if(v == '03'){
                text = '提现中';
            }else if(v == '04'){
                text = '提现成功';
            }else if(v == '05'){
                text = '提现失败';
            }else if(v == '06'){
                text = '失败';
            }else if(v == '07'){
                text = '支付完毕';
            }

            return text;
        }
    })
    .filter('remotePercent', function(){
        return function(v){
            if(v){
                return  v = v.replace('%','');
            }

        }
    })
    .filter('secondToSymbol', function() {
      return function (v) {
        var f = parseInt(v/60);
        var s = v%60;
        f = f < 10 ? "0"+f : f;
        s = s < 10 ? "0"+s : s;
        //return f+"′"+s+"″";
        return '00:'+f+":"+s;
      }
    })
    .filter('secondToInt', function() {
      return function (v) {
        var s = Math.ceil(v/60);
        return s;
      }
    })
    .filter('visitorType', function() {
      return function (v) {
          var r = '店铺';
          if(v){
              if(v == '08'){
                  r = '活动';
              }else if(v == '07'){
                  r = '活动详情';
              }else{
                  r = '产品';
              }
          }
          return r;
      }
    })
    .filter('getUserHeadImg', function() {
        return function (v) {
            var r = reqUrl;
            if(v){
                if(v.indexOf('http')>-1){
                    r = v;
                }else{
                    r = reqUrl+v;
                }
            }
            return r;
        }
    })
    .directive('textarea', function(){
        return {
            restrict: 'E',
            scope: {
                'noIonic': '='
            },
            link: function(scope, element, attr){
                if(scope.noIonic){
                    element.bind('touchend  touchmove touchstart', function(e){
                        e.stopPropagation();
                    });
                }
            }
        }
    })
    .directive('dateFormat',function($filter){
        return {
            require: 'ngModel',
            link: function(scope, elem, attr, ngModelCtrl) {
                ngModelCtrl.$formatters.push(function(modelValue){
                    if(modelValue) {
                        return new Date(modelValue);
                    } 
                });

                ngModelCtrl.$parsers.push(function(value){
                    if(value) {
                        return $filter('date')(value, 'yyyy-MM-dd');
                    }
                });
            }
        };
    })
    .filter('newProfitFudongRender', function() {
        return function(rate, flag) {
            rate = rate || 0;
            if (/^[\d.]+$/.test(rate)){
                return "";
            }
            if(rate.indexOf("浮动")>=0){
                return "+浮动";
            }
            return "";
        }
    })
    .filter('newProfitRender', function() {
        return function(rate, flag) {
            rate = rate || 0;
            if (flag && rate <= 0) return "-";
            if (rate > 100) rate = 100;
            if (/^[\d.]+$/.test(rate)){
                return (rate || 0) + "";
            }else if(rate.indexOf("浮动")){
                return rate.split("\+")[0];
            }else{
                return rate;
            }
        }
    })
    .filter('newProfitFudongRender', function() {
        return function(rate, flag) {
            rate = rate || 0;
            if (/^[\d.]+$/.test(rate)){
                return "";
            }
            if(rate.indexOf("浮动")>=0){
                return "+浮动";
            }
            return "";
        }
    })
    .filter('orderPhoStatus', function() {
        return function(status) {

            //（01:未提交审核 02提交审核  未审核  03:提交审核 审核成功  04:提交审核 审核失败）
            if (status == "01" ) {
                return "待上传";
            } else if (status == "02") {
                return "审核中";
            } else if (status == "03") {
                return "审核通过";
            } else if (status == "04") {
                return "退回";
            }
            else {}
        }
    })
    .filter('orderContractStatus', function() {
        return function(status) {
            //合同状态：
            //前端 —— 后台
            //待申请 待申请
            //待处理 已申请
            //待寄出 待寄出
            //待回收 已寄出
            //待回收 待回收
            //已回收 已回收
            //（"01"; //待申请 "02"; //已申请 "03"; //已寄出 "04"; //已回收）
            if (status == "01" ) {
                return "待申请";
            } else if (status == "02") {
                return "待处理";
            } else if (status == "03") {
                return "待寄出";
            } else if (status == "04") {
                return "待回收";
            }else if (status == "05") {
                return "已回收";
            }

        }
    })
    .filter('contentwrap', function($sce) {
        return function(val) {
            if(val !=null && val!=''){

                return $sce.trustAsHtml(val.replaceAll('\n', '<br>'));
            }
            return '';
        }
    })
    .filter('pevcQrRevenue', function($sce) {
        return function(val) {
            if(val!=undefined && val !=null && val!=''){
                if(val.indexOf('%+浮动') != -1){
                    val = val.replace('%+浮动', '') + '<span>%+浮动</span>';
                }else
                if(val.indexOf('%-浮动') != -1){
                    val = val.replace('%-浮动', '') + '<span>%-浮动</span>';
                }else
                if(val.indexOf('%') != -1 && val.indexOf('%-浮动') == -1){
                    val = val.replace('%', '') + '<span>%</span>';
                }

                $sce.trustAsHtml(val)
                return val;
            }

            return '-';
        }
    })
    .filter('proNumber', function($sce) {
        return function(value) {

            var temp;

            if (value < 10000) {
                temp = value + '元';
            } else if (value >= 10000 && value < 100000000) {
                temp = value / 10000 + '<span>万</span>';
            }
            // else if (value >= 1000000 && value < 10000000) {
            //     temp = value / 1000000 + '百万';
            // } else if (value >= 10000000 && value < 100000000) {
            //     temp = value / 10000000 + '千万';
            // }
            else if (value >= 100000000 && value < 100000000000) {
                temp = value / 100000000 + '<span>亿</span>';
            }
            // else if (value >= 10000000000) {
            //     temp = value / 10000000000 + '百亿';
            // } else if (value >= 100000000000){
            //     temp = value / 100000000000 + '千亿';
            // }

            $sce.trustAsHtml(temp)
            return temp;
        }
    })
    .filter('proSelfDuetime', function($sce) {
        return function(value) {
            if(value) {
                var arr = value.split("");
                var val = ''
                for(var i=0; i<arr.length; i++) {
                    if(isNaN(arr[i])) {
                        val += "<span>"+arr[i]+"</span>";
                    } else {
                        val += arr[i];
                    }
                }
                return $sce.trustAsHtml(val);
            }
            return "";
        }
    })
    .filter('dapanFloat', function($sce) {
        return function(value) {
            if(value) {
                value = value.toString();
                var flarr = value.split('.');
                if(flarr.length ==2) {
                    var fl = flarr[1];//小数部分
                    var xsarr = fl.split('');
                    if(xsarr.length>2) {
                        return flarr[0]+"."+fl.substr(0, 2);
                    }
                }
            }
            return value;
        }
    })

    .filter('to_trusted_zx', function($sce) {
        return function(text) {
            if(text.length>40) {
                text = text.substr(0, 40) +"...";
            }
            return $sce.trustAsHtml(text);
        };
    })
    .filter('coverPhone',function(){
        return function(v){
                return v.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
        }
    })
;

